package com.dms.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dms.bean.Fee;
import com.dms.bean.Student;
import com.dms.dao.FeeDao;
import com.dms.model.FeeModel;

@WebServlet("/FeeServlet")
public class FeeServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7741598637555321424L;
	/**
     * @see HttpServlet#HttpServlet()
     */
    public FeeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 response.setContentType("text/html");
		 response.setCharacterEncoding("UTF-8");
	   	 request.setCharacterEncoding("UTF-8");
	   	HttpSession session = request.getSession();
		Student stu = (Student) session.getAttribute("student");
	   	String action = request.getParameter("act");
		if (action == null)
			action = "";
		if (action.equals("add")) {
			this.add(request, response,stu);
		}
		if (action.equals("list")) {
			this.list(request, response,stu);
		}
		if (action.equals("edit")) {
			this.edit(request, response,stu);
		}
		if (action.equals("update")) {
			this.update(request, response,stu);
		}
		if (action.equals("info")) {
			this.info(request, response,stu);
		}
		if (action.equals("delete")) {
			this.delete(request, response,stu);
		}
	}
	
	protected void add(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		
		int month = Integer.parseInt(request.getParameter("month"));
		String dormID = request.getParameter("dormID");
		String money = request.getParameter("money");
		if (dormID == null || "".equals(dormID) || money == null || "".equals(money) || month < 1 || month > 12) {
			request.setAttribute("message", "天哪噜,缺数据或数据错误(⊙o⊙)…");
			request.getRequestDispatcher("addFee.jsp").forward(request, response);
		}
		boolean val = FeeModel.add(month, dormID, money);
		if (!val) {
			request.setAttribute("message", "天哪噜,失败啦(⊙o⊙)…");
			request.getRequestDispatcher("addFee.jsp").forward(request, response);
		} else {
			response.sendRedirect("FeeServlet?act=list");
		}
	}
	protected void list(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		List<Fee> fees = null;
		if(!stu.getStuID().equalsIgnoreCase("admin"))
		{
			 fees = FeeModel.list2(stu.getStuID());
		}
		else
		{
		 fees = FeeDao.list();
		}
		request.setAttribute("list", fees);
		request.getRequestDispatcher("fee.jsp").forward(request, response);
	}
	protected void edit(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int feeID = Integer.parseInt(request.getParameter("feeID"));
		Fee fee =  FeeDao.find(feeID);
		request.setAttribute("fee", fee);
		request.getRequestDispatcher("editFee.jsp").forward(request, response);
	}
	protected void update(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int month = Integer.parseInt(request.getParameter("month"));
		String dormID = request.getParameter("dormID");
		String money = request.getParameter("money");
		int state = Integer.parseInt(request.getParameter("state"));
		if (dormID == null || "".equals(dormID) || money == null || "".equals(money) || month < 1 || month > 12) {
			request.setAttribute("message", "天哪噜,缺数据或数据错误(⊙o⊙)…");
			request.getRequestDispatcher("addFee.jsp").forward(request, response);
		}
		int feeID = Integer.parseInt(request.getParameter("feeID"));
		Fee fee = new Fee();
		fee.setFeeID(feeID);
		fee.setDormID(dormID);
		fee.setMonth(month);
		fee.setMoney(money);
		fee.setState(state);
		boolean var =false;
		try {
		      var =  FeeDao.save(fee);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!var){
			request.setAttribute("message", "修改失败 @_@");
			request.getRequestDispatcher("editFee.jsp").forward(request, response);
		}else{
			response.sendRedirect("FeeServlet?act=list");
		}
	}
	protected void info(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("feeID"));
		Fee fee = FeeDao.find(id);
		request.setAttribute("fee", fee);
		request.getRequestDispatcher("feeInfo.jsp").forward(request, response);
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("feeID"));
		boolean var =false;
		try {
		      var =  FeeDao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!var){
			request.setAttribute("message", "删除失败 @_@");
			request.getRequestDispatcher("editFee.jsp").forward(request, response);
		}else{
			response.sendRedirect("FeeServlet?act=list");
		}
	}

}
